#include "raftRpcUtil.h"
#include <mprpcchannel.h>
#include <mprpccontroller.h>

namespace raftCore 
{
bool RaftRpcUtil::AppendEntries(raftRpcProctoc::AppendEntriesArgs *args, 
                                raftRpcProctoc::AppendEntriesReply *response) {
    rpc::MprpcController controller;
    m_stub->AppendEntries(&controller, args, response, nullptr);
    return !controller.Failed();
}

bool RaftRpcUtil::InstallSnapshot(raftRpcProctoc::InstallSnapshotRequest *args,
                                  raftRpcProctoc::InstallSnapshotResponse *response) {
    rpc::MprpcController controller;
    m_stub->InstallSnapshot(&controller, args, response, nullptr);
    return !controller.Failed();
}

bool RaftRpcUtil::RequestVote(raftRpcProctoc::RequestVoteArgs *args, 
                            raftRpcProctoc::RequestVoteReply *response) {
    rpc::MprpcController controller;
    m_stub->RequestVote(&controller, args, response, nullptr);
    return !controller.Failed();
}

//先开启服务器，再尝试连接其他的节点，中间给一个间隔时间，等待其他的rpc服务器节点启动
RaftRpcUtil::RaftRpcUtil(std::string ip, short port) {
    //发送rpc设置
    m_stub = new raftRpcProctoc::raftRpc_Stub(new rpc::MprpcChannel(ip, port, true));
}

RaftRpcUtil::~RaftRpcUtil() { delete m_stub; }

}   // namespace raftCore
